/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.ws.client.program;

import junit.framework.TestCase;
import net.bioteam.appweb.TestConfig;
import net.bioteam.appweb.model.Program;
import net.bioteam.appweb.model.ProgramBuilder;
import net.bioteam.appweb.submit.ProgramValueBean;
import net.bioteam.appweb.user.User;
import net.bioteam.appweb.ws.WsClient;

public class OtherProgramsTest extends TestCase
{

	public OtherProgramsTest()
	{
		super();
	}
	public OtherProgramsTest(String name)
	{
		super(name);
	}

	public WsClient client;
	protected void setUp() throws Exception
	{
		TestConfig.init();
		User user=new User();
		user.setName(TestConfig.username);
		user.setPassword(TestConfig.password);
		client=new WsClient(user, TestConfig.serviceURL, null);
	}
	public static void main(String args[]) throws Exception {
		OtherProgramsTest test=new OtherProgramsTest();
		test.setUp();
		
		
//		Method[] mss=test.getClass().getMethods();
//		for (Method ms: mss)
//		{
//			if (ms.getName().startsWith("test"))
//			{
//				try
//				{
//					System.out.println(ms.getName());
//					ms.invoke(test);
//				//break;
//				} catch (Exception e)
//				{
//					e.printStackTrace();
//				}
//			}
//		}
		for (int i=0; i<3; i++)
		{
			test.testEcho();
			Thread.sleep(1000);
		}
		TestConfig.shutdown();
		
	}
	public void testEcho() throws Exception
	{
		Program p=ProgramBuilder.getProgram("echo");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("sleep_time", "60");
		//bean.setValue("second", "6");
		//bean.setValue("freq", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testRdemo() throws Exception
	{
		Program p=ProgramBuilder.getProgram("rdemo");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("first", "5");
		bean.setValue("second", "6");
		//bean.setValue("freq", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testRpad() throws Exception
	{
			
		Program p=ProgramBuilder.getProgram("rpad");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("rscript", "inputs/rpad.r");
		
		//bean.setValue("freq", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testPlink() throws Exception
	{
		Program p=ProgramBuilder.getProgram("plink");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("ped", "inputs/test.ped");
		bean.setValue("map", "inputs/test.map");
		bean.setValue("freq", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testTIGR_Assembler() throws Exception
	{
		Program p=ProgramBuilder.getProgram("TIGR_Assembler");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("sequence", "inputs/201.seq");
		bean.setValue("contig_file", "inputs/201.contigs");
		bean.setValue("quality_file", "inputs/201.qual");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testLucy() throws Exception
	{
		Program p=ProgramBuilder.getProgram("lucy");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("vector_splice", "inputs/PUC19splice");
		bean.setValue("vector_seq", "inputs/PUC19");
		bean.setValue("sequence", "inputs/atie.seq");
		bean.setValue("sequence2", "inputs/atie.2nd");
		bean.setValue("quality_file", "inputs/atie.qul");
		bean.setValue("output_seq", "lucy.seq");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testWise2() throws Exception
	{
		Program p=ProgramBuilder.getProgram("wise2");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("protein", "inputs/rrm.HMM");
		bean.setValue("dna", "inputs/hn_est.fa");
		bean.setValue("wise2", "estwise");
		bean.setValue("hmmer", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testMrBayes() throws Exception
	{
		Program p=ProgramBuilder.getProgram("MrBayes");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("nexus_file", "inputs/mrbayes.nex");
		//bean.setValue("imc", "inputs/build_icm.out");
		//bean.setValue("datafile", "inputs/error.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testGlimmer() throws Exception
	{
		Program p=ProgramBuilder.getProgram("glimmer3");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("sequence", "inputs/tpall.fna");
		bean.setValue("imc", "inputs/build_icm.out");
		//bean.setValue("datafile", "inputs/error.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testExtract() throws Exception
	{
		Program p=ProgramBuilder.getProgram("extract");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("sequence", "inputs/tpall.fna");
		bean.setValue("list_coord", "inputs/long-orfs.out");
		//bean.setValue("datafile", "inputs/error.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testLong_orfs() throws Exception
	{
		Program p=ProgramBuilder.getProgram("long-orfs");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("sequence", "inputs/tpall.fna");
		//bean.setValue("mapfile", "inputs/error.map");
		//bean.setValue("datafile", "inputs/error.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testBuild_icm() throws Exception
	{
		Program p=ProgramBuilder.getProgram("build-icm");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("list_orfs", "inputs/extract.out");
		//bean.setValue("mapfile", "inputs/error.map");
		//bean.setValue("datafile", "inputs/error.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testPedwipe() throws Exception
	{
		Program p=ProgramBuilder.getProgram("pedwipe");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("pedfile", "inputs/error.ped");
		bean.setValue("mapfile", "inputs/error.map");
		bean.setValue("datafile", "inputs/error.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testPedmerge() throws Exception
	{
		Program p=ProgramBuilder.getProgram("pedmerge");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("base1", "assoc");
		bean.setValue("base2", "merlin-assoc-covars");
		bean.setValue("base3", "output");
		bean.setValue("inputfile1", "inputs/assoc.ped");
		bean.setValue("inputfile2", "inputs/assoc.dat");
		bean.setValue("inputfile3", "inputs/merlin-assoc-covars.ped");
		bean.setValue("inputfile4", "inputs/merlin-assoc-covars.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testPedstats() throws Exception
	{
		Program p=ProgramBuilder.getProgram("pedstats");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("pedfile", "inputs/asp.ped");
		bean.setValue("mapfile", "inputs/asp.map");
		bean.setValue("datafile", "inputs/asp.dat");
		//bean.setValue("npl", "1");
		//bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testMerlinRegress() throws Exception
	{
		Program p=ProgramBuilder.getProgram("merlin-regress");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("pedfile", "inputs/asp.ped");
		bean.setValue("mapfile", "inputs/asp.map");
		bean.setValue("datafile", "inputs/asp.dat");
		bean.setValue("mean", "0.0");
		bean.setValue("variance", "1.5");
		bean.setValue("Heritability", "0.8");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	
	public void testMerlin() throws Exception
	{
		Program p=ProgramBuilder.getProgram("merlin");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("pedfile", "inputs/asp.ped");
		bean.setValue("mapfile", "inputs/asp.map");
		bean.setValue("datafile", "inputs/asp.dat");
		bean.setValue("npl", "1");
		bean.setValue("pairs", "1");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
		bean=new ProgramValueBean(p);
		bean.setValue("pedfile", "inputs/assoc.ped");
		bean.setValue("mapfile", "inputs/assoc.map");
		bean.setValue("datafile", "inputs/assoc.dat");
		bean.setValue("custom", "inputs/assoc.tbl");
		bean.setValue("assoc", "assoc");
		ret=client.runProgram(bean);
		postJobProcess(ret);
		
	}

	public void testPrep_and_loki() throws Exception
	{
		Program p=ProgramBuilder.getProgram("prep_and_loki");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("control_file", "inputs/control_jv");
		bean.setValue("param_file", "inputs/param_jv");
		bean.setValue("datafile2", "inputs/jvped");
		bean.setValue("datafile3", "inputs/seedfile");
		bean.setValue("datafile4", "inputs/test_seedfile");
		String[] ret=client.runProgram(bean);
		
		postJobProcess(ret);
		
	}
	public void testLoki_ext() throws Exception
	{
		Program p=ProgramBuilder.getProgram("loki_ext");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("loki_out", "inputs/loki.out");
	//	bean.setValue("loki_pos", "inputs/loki.pos");
		//bean.setValue("assocCC", "1");
		//bean.setValue("assocTDT", "1");
		String[] ret=client.runProgram(bean);
		postJobProcess(ret);
		
	}

	public void testLoki_count() throws Exception
	{
		Program p=ProgramBuilder.getProgram("loki_count");
		ProgramValueBean bean=new ProgramValueBean(p); if (TestConfig.clusterid!=null) 	{bean.setValue("_cluster", TestConfig.clusterid);}
		bean.setValue("loki_out", "inputs/loki.out");
		bean.setValue("loki_pos", "inputs/loki.pos");
		//bean.setValue("assocCC", "1");
		//bean.setValue("assocTDT", "1");
		String[] ret=client.runProgram(bean);
		postJobProcess(ret);
		
	}
	
	public void testHaploview() throws Exception
	{
		Program p=ProgramBuilder.getProgram("haploview");
		ProgramValueBean bean = new ProgramValueBean(p);
		if (TestConfig.clusterid != null)
		{
			bean.setValue("_cluster", TestConfig.clusterid);
		}
		bean.setValue("pedfile", "inputs/haploview_sample.ped");
		bean.setValue("info", "inputs/haploview_sample.info");
		bean.setValue("assocCC", "1");
		bean.setValue("assocTDT", "1");
		String[] ret=client.runProgram(bean);
		postJobProcess(ret);
		
	}
	

	protected void postJobProcess(String ret[])
	{
		String jobid=ret[0];
		TestConfig.logger.info("job id is "+jobid);
		TestConfig.logger.info("cmdline is "+ret[2]);
	}
	
	

}
